﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace FPTPages.NewsPage
{
    public partial class ReadNews : System.Web.UI.Page
    {
        public int postId;
        protected void Page_Load(object sender, EventArgs e)
        {
            postId = int.Parse(Request["postId"]);
        }

        public string GetPost()
        {
            
            StringBuilder builder = new StringBuilder();
            string postHTML = "";
            DataSet ds = new DataSet();

            string sql = "select * from Posts where PostId = " + postId;
            string url = ConfigurationManager.ConnectionStrings["SqlProviderCon"].ToString();

            using (SqlConnection con = new SqlConnection(url))
            {
                using (SqlDataAdapter da = new SqlDataAdapter(sql,con))
                {
                    da.Fill(ds);
                }
            }

            foreach (DataRow item in ds.Tables[0].Rows)
            {
                builder.Append(@"<h2 class=""title"">" + item[1].ToString() + " </h2>");
                builder.Append(@"<p>Post by VuPTT</p>");
                builder.Append(@"</br>");
                builder.Append(@"<p>" + item[2].ToString() + "</p>"); 
            }

            postHTML = builder.ToString();
            return postHTML;
        }
        public string GetOtherNews()
        {
            string str = "";
            StringBuilder builder = new StringBuilder();
            DataSet ds = new DataSet();

            string sql = "select top 10 PostTitle,PostID from Posts ORDER BY DatePost";
            string url = ConfigurationManager.ConnectionStrings["ProjectDatabaseConnectionString2"].ToString();

            using (SqlConnection con = new SqlConnection(url))
            {
                using (SqlDataAdapter da = new SqlDataAdapter(sql, con))
                {
                    da.Fill(ds);
                }
            }

            foreach (DataRow item in ds.Tables[0].Rows)
            {
                builder.Append(@"<li><a href=""ReadNews.aspx?postid= " + item[1].ToString() +
                    @" ""> " + item[0].ToString().Trim() + "</a></li>\n");
            }

            str = builder.ToString();

            return str;
        }
    }
}